using Fusion.Frameworks.Assets;
using Fusion.Utilities.IO;
using Fusion.Utilities.Singleton;
using LitJson;
using System;
using System.IO;
using UnityEngine;

namespace Fusion.Frameworks.Version
{
    [Serializable]
    public class VersionData {
        [SerializeField]
        private uint large = 0;
        [SerializeField]
        private uint middle = 0;
        [SerializeField]
        private uint small = 0;
        [SerializeField]
        private uint resources = 0;
        public uint Large { get => large; set => large = value; }
        public uint Middle { get => middle; set => middle = value; }
        public uint Small { get => small; set => small = value; }
        public uint Resources { get => resources; set => resources = value; }

        public VersionData() { }
        public VersionData(uint large, uint middle, uint small, uint resources)
        {
            Large = large;
            Middle = middle;
            Small = small;
            Resources = resources;
        }

        public VersionData(string version)
        {
            string[] versionStr1 = version.Split(".");
            string[] versionStr2 = versionStr1[2].Split("_R");

            large = uint.Parse(versionStr1[0]);
            middle = uint.Parse(versionStr1[1]);
            small = uint.Parse(versionStr2[0]);
            resources = uint.Parse(versionStr2[1]);
        }
        public override string ToString()
        {
            return $"{large}.{middle}.{small}_R{resources}";
        }
    }


    public class VersionManager : Singleton<VersionManager>
    {
        private VersionData versionData = null;

        public VersionData GetVersionData()
        { 
            if (versionData == null)
            {

                string versionJsonFilePath = Path.Combine(AssetsManager.PersistentLoadPath, "Version.json");
                string json = IOUtility.Read(versionJsonFilePath);
                if (json == null)
                {
                    versionJsonFilePath = Path.Combine(AssetsManager.StreamingLoadPath, "Version.json");
                    json = IOUtility.Read(versionJsonFilePath);
                }

                if (json != null)
                {
                    versionData = JsonMapper.ToObject<VersionData>(json);
                }
                else
                {
                    versionData = new VersionData();
                }
            }
            return versionData;
        }

    }
}


